<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\common\controller\Backend;

class Product extends Backend
{
    public function __construct()
    {
        parent::__construct();
        $this->ProductModel = model('common/Product/Product');
        $this->TypeModel = model('common/Product/Type');
    }

    public function index()
    {
        ini_set("error_reporting", "E_ALL & ~E_NOTICE");
        //每页显示多少条数据
        $limit = 10;

        //查询数据总数
        $count = $this->ProductModel->count();

        //分页查询
        $ProductModel = $this->ProductModel->order('id', 'asc')->paginate($limit);

        // return json($ProductModel);
        // exit;

        $this->assign([
            'count' => $count,
            'productList' => $ProductModel,
        ]);

        return $this->fetch();
    }

    //添加商品
    public function add()
    {
        $typeList = $this->TypeModel->select();

        //判断是否有表单提交
        if ($this->request->isPost()) {
            $params = $this->request->param();

            //查询是否有这个类型的商品
            $TypeModel = $this->TypeModel->find();

            if (!$TypeModel) {
                $this->error('没有该类型的商品');
                exit;
            }

            $data = [
                'typeid' => $params['type'],
                'name' => $params['name'],
                'price' => $params['price'],
                'stock' => $params['stock'],
                'content' => $params['content'],
            ];

            // $productType = '';
            // switch ($data['typeid']) {
            //     case 1:
            //         $productType
            // }
        }

        $this->assign([
            'typeList' => $typeList
        ]);

        return $this->fetch();
    }
}
